// pages/index/sign-in.js
var app = getApp();
var util = require('../../utils/util.js');
var _this, pageNo = 1, pages = '', limit = 10;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //地址
    appLink: app.globalData.appLink, //,
    // 链接判断
    buttonClicked: false,
    optionBox: false,
    region: ['重庆', '重庆', '渝北区'],
    repSubmit: true,
    index: '',
    getConvert: false,
    newList: [],
    deliverType: 'ONLINE'
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    _this.init();
  },
  onShareAppMessage: function () {

  },
  init:function(){
    // 获取日期等个人信息
    _this.setData({
      userInfo: app.globalData.programUserInfo
    })
    // 获取奖品列表
    _this.list(1,10);
    _this.signData();
    _this.signPoints();
  },
  /*
    *签到
    */
  // 签到日期
  signData:function(){
    let data = {};
    data.userId = _this.data.userInfo.id;
    // 签到日期
    app.getMassage(data, null, 'userSign', function (res) {
      let signDate = res.data;
      for (let i = 0; i < signDate.length; i++){
        let today = util.formatTime(new Date).split(' ')[0],
        date = signDate[i].signDate.split(' ')[0],
        month = date.split('-')[1],
        day = date.split('-')[2];
        if (today == date){
          signDate[i].signDate = '今日';
        }else{
          signDate[i].signDate = month + '-' + day;
        }
      }
      _this.setData({
        signData: signDate
      })
    }, 'GET')
  },
  // 积分
  signPoints:function(){
    let data = {};
    data.userId = _this.data.userInfo.id;
    // 积分
    app.getMassage(data, null, 'signPoints', function (res) {
      _this.setData({
        signPoints: res.data
      })
    }, 'GET')
  },
  // 签到
  signBtn: function (e) {
    let index = e.currentTarget.dataset.index,
      signData = _this.data.signData,
      canSign = signData[index].canSign,
      isSign = signData[index].isSign,
      signDate = signData[index].signDate.split(' ')[0],
      today = util.formatTime(new Date).split(' ')[0];
    // if (canSign && signDate == today){
    if (!isSign && canSign){
      let data = {};
      data.userId = _this.data.userInfo.id;
      app.getMassage(data, null, 'sign', function (res) {
        _this.signData();
        _this.signPoints();
        util.toastMsg('签到成功','success');
      }, 'GET')
    }
  },


  /*
   *获取列表
  */ 
  // 获取列表
  list: function (page, lim) {
    var url = "userSign";
    app.getList(page, lim, null, url, function (res) {
      if (res.data) {
        if (page == 1) {
          _this.setData({
            newList: [],
          })
        }
        pageNo = res.data.page / 1 + 1;
        pages = res.data.pages;
        var list = res.data.list;
        var newList = _this.data.newList;
        newList = newList.concat(list);
        _this.setData({
          newList: newList,
          pageNo: pageNo,
          pages: pages
        })
      }
    })
  },
  // 滚动触底
  scrolltolower: function () {
    // 判断是否是最后一页
    if (pages >= pageNo) {
      _this.list(pageNo, limit);
    }
  },
  /*
  * 弹窗信息
  */
  // 地址
  bindRegionChange: function (e) {
    _this.setData({
      region: e.detail.value
    })
  },
  // 输入信息
  massageChange: function (e) {
    var userType = e.currentTarget.dataset.type
    var massage = e.detail.value; // userLeaveMassage 留言
    switch (userType) {
      case 'name':
        _this.setData({ name: massage })
        break;
      case 'phone':
        _this.setData({ phone: massage })
        break;
      case 'address':
        _this.setData({ address: massage })
        break;
    }
  },
  // 验证信息
  verifyMessage: function (font) {
    var region = _this.data.region, // 地区
      name = _this.data.name, // 名字
      phone = _this.data.phone, // 电话
      address = _this.data.address; // 详细地址
    // 判断是否有名字
    if (name == null || name == '') {
      util.modalMsg('提示', font + '请输入名字', function () {
        return false;
      }, null, false);
      return false;
    }
    // 判断电话号码是否正确
    if (phone == null || phone == '') {
      util.modalMsg('提示', font + '输入手机号', function () {
        return false;
      }, null, false);
      return false;
    } else {
      if (!util.testPhone(phone)) {
        util.modalMsg('提示', font + '输入正确的手机号', function () {
          return false;
        }, null, false);
        return false;
      }
    }
    // 判断是否有地区
    if (region == null || region == '') {
      util.modalMsg('提示', font + '请选择地区', function () {
        return false;
      }, null, false);
      return false;
    }
    // 判断是否有详细地址
    if (address == null || address == '') {
      util.modalMsg('提示', font + '请输入详细地址', function () {
        return false;
      }, null, false);
      return false;
    }
    return true;
  },
  convert: function () {
    wx.showLoading({
      title: '请稍后',
    })
    let deliverType = _this.data.deliverType;
    if (deliverType == 'ONLINE'){
      if (!_this.verifyMessage('兑换礼品需要')) {
        return false;
      }
    }
    if (!_this.data.repSubmit) {
      return false;
    }
    _this.setData({
      repSubmit: false
    })
    let data = {};
    let region = _this.data.region; // 地区
    let index = _this.data.chooseGoods;
    data.userId = _this.data.userInfo.id;
    data.productId = _this.data.newList[index].id;
    data.deliverType = deliverType;
    if (deliverType == 'ONLINE'){
      data.contact = _this.data.name; // 名字
      data.contactPhone = _this.data.phone; // 电话
      data.province = region[0];
      data.city = region[1];
      data.area = region[2];
      data.detail = _this.data.address; // 详细地址
    }
    app.getMassage(data, null, 'exchange', function (res) {
      wx.hideLoading();
      if(res.data){
        util.toastMsg('兑换成功','success');
        _this.setData({
          getConvert: false,
          deliverType: "ONLINE"
        })
        _this.signPoints();
      }else{
        util.toastMsg('兑换失败稍后再试', 'none');
        _this.setData({
          getConvert: false,
          deliverType: "ONLINE"
        })
        _this.signPoints();
      }
    })
  },// 优惠卷
  closePop: function () {
    _this.setData({
      getConvert: false,
      deliverType: "ONLINE"
    })
  },
  openPop:function(e){
    let signPoints = _this.data.signPoints,
      points = e.currentTarget.dataset.points;
    if (signPoints >= points){
      _this.setData({
        getConvert: true,
        chooseGoods: e.currentTarget.dataset.index
      })
    }else{
      util.toastMsg('积分不够', 'none');
    }
  },
  // 活动规则
  goToProtocol:function(){
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateTo({
        url: '/pages/h5/protocol',
      })
    }
  },
  radioChange:function(e){
    let val = e.detail.value;
    _this.setData({
      deliverType: val
    })
  }
})